// pages/User/ProjectBrokerage/ProjectBrokerage.js

const app = getApp();
import { queryUserSettlementGather } from '../../../api/project';
import { throttle } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalAmount: 0.0, // 项目佣金总金额
    startTime: '', // 开始时间
    endTime: '', // 结束时间
    page: 1,
    dataList: [],
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
    shareTitle: app.globalData.global.shareTitle,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
    })
    this.getData()
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    if(this.data.isReachBottom){
      this.getData()
    }
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Tabbar/Home/Home?scene='+userId,
    }
  },
  getData(){
    let _this = this;
    queryUserSettlementGather(_this.data.startTime,_this.data.endTime,_this.data.page)
    .then( res => {
      if(res.success){
        var list = _this.data.dataList;
        if(_this.data.page == 1){
          list = res.data.settlements;
        }else{
          list.concat(res.data.settlements);
        }
        _this.setData({
          dataList: list,
          totalAmount: res.data.totalAmount ?? 0.0,
          page: _this.data.page+1,
        })
        if(_this.data.page * 10 >= res.data.totalCount){
          _this.setData({
            isReachBottom: false,
            noMore: true,
            loading: false,
            loadingFailed: false,
          })
        }else{
          _this.setData({
            isReachBottom: true,
            noMore: false,
            loading: false,
            loadingFailed: false,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 展示时间选择器
  showPickTime(){
    const child = this.selectComponent('#pickDateTime')
    child.showPickTime();
  },
  // 时间选择器触发
  pickdateevent(e){
    console.log(e,'选择时间')
    this.setData({
      startTime: e.detail.startTime,
      endTime: e.detail.endTime,
      page: 1,
    })
    this.getData();
  },
})